package net.biz.reminder;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.biz.reminder.util.JDBCOracleUtil;

public class Reminder {

	/**
	 * 提示程序，调度程序在外面
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 1.每次定时执行
		System.out.println("====>"+new Date());
		// 2.查询ADM_REMIND表
		try {
			List<Map<String, Object>> result = JDBCOracleUtil
					.executeQuery("SELECT * FROM ADM_REMIND WHERE VALID = '1'");
			// 3.执行存储过程，获得结果，存储过程默认会将ID传入
			Iterator<Map<String, Object>> it = result.iterator();
			while (it.hasNext()) {
				// 3.1.获取提醒算法行
				Map<String, Object> row = it.next();
				String id = String.valueOf(row.get("ID"));
				String mail = String.valueOf(row.get("MAIL"));
				String program = String.valueOf(row.get("PROGRAM"));
				String memo = String.valueOf(row.get("MEMO"));
				// 3.2调用存储过程
				List inList = new ArrayList();
				List<Object> outList = new ArrayList<Object>();
				inList.add(id);
				JDBCOracleUtil.callProcduce(inList, outList, program);
				String appCode = String.valueOf(outList.get(0));
				String errMsg = String.valueOf(outList.get(1));
				if ("1".equals(appCode)) {
					// 如果执行成功，则启用另外线程发送邮件
					// 4.发电子邮件给列表中的人
					MailSender ms = new MailSender(id);
					new Thread(ms).start();
				} else {
					// 获取执行结果，如果执行结果是失败，则输出错误信息
					System.out.println(errMsg);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

}
